
package com.jf.cloud.supplier.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.supplier.constant.SupplierStatus;
import com.jf.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.dto.AuditingSupplierInfoDTO;
import com.jf.cloud.supplier.dto.SupplierAuditingDTO;
import com.jf.cloud.supplier.service.SupplierAuditingService;
import com.jf.cloud.supplier.service.SupplierDetailService;
import com.jf.cloud.supplier.vo.SupplierAuditingInfoVO;
import com.jf.cloud.supplier.vo.SupplierAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 商家审核信息
 *
 * @author zz
 * @date 2021-02-01 17:10:35
 */
@RestController("platformSupplierAuditingController")
@RequestMapping("/p/supplier_auditing")
@Tag(name = "platform-平台审核信息")
public class SupplierAuditingController {

    @Value("${jfcloud.expose.permission:}")
    private Boolean permission;

    @Autowired
    private SupplierAuditingService supplierAuditingService;

    @Autowired
    private SupplierDetailService supplierDetailService;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @GetMapping("/page")
    @Operation(summary = "分页查询" , description = "分页查询")
    public ServerResponseEntity<PageVO<SupplierAuditingInfoVO>> getSupplierAuditingPage(PageDTO pageDTO, @ParameterObject AuditingSupplierInfoDTO auditingInfoDTO) {
        checkAuthorized();
        return ServerResponseEntity.success(supplierAuditingService.auditingInfoPage(pageDTO, auditingInfoDTO));
    }

    @GetMapping("/{supplierId}")
    @Operation(summary = "根据供应商id查询详情信息" , description = "根据供应商id查询详情信息")
    public ServerResponseEntity<SupplierAuditingVO> getSupplierAuditing(@PathVariable Long supplierId) {
        checkAuthorized();
        SupplierAuditingVO supplierAuditing = supplierAuditingService.getBySupplierId(supplierId);
        if (Objects.isNull(supplierAuditing)){
            supplierAuditing = new SupplierAuditingVO();
        }
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        if (BooleanUtil.isFalse(permission)){
            supplierApiDetailVO.setContactPhone(PhoneUtil.hideBetween(supplierApiDetailVO.getContactPhone()).toString());
            if (PrincipalUtil.isMobile(supplierApiDetailVO.getSupplierAccount())){
                supplierApiDetailVO.setSupplierAccount(PhoneUtil.hideBetween(supplierApiDetailVO.getSupplierAccount()).toString());
            }
        }
        if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE.value())) {
            supplierAuditing.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OPEN_AWAIT_AUDIT.value())) {
            supplierAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(supplierId, SysTypeEnum.SUPPLIER.value());
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            supplierApiDetailVO.setSupplierAccount(authAccountVO.getPhone());
            supplierApiDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        supplierAuditing.setSupplierDetail(supplierApiDetailVO);
        return ServerResponseEntity.success(supplierAuditing);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核信息(审核供应商)" , description = "审核信息(审核供应商)")
    public ServerResponseEntity<Void> audit(@Valid @RequestBody SupplierAuditingDTO supplierAuditingDTO) {
        checkAuthorized();
        supplierDetailService.audit(supplierAuditingDTO);
        supplierDetailService.removeCacheBySupplierId(supplierAuditingDTO.getSupplierId());

        return ServerResponseEntity.success();
    }

    /**
     * 校验权限
     */
    private void checkAuthorized () {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
    }
}
